{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.path.append(\"..\")\n",
    "from utils import load_obj, save_obj\n",
    "from settings import cls_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from static_var import llm_infer_result_pkl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'../data/pkl/infer_cls1_result.pkl'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm_infer_result_pkl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "outputs = load_obj(llm_infer_result_pkl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200000"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(outputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"<|im_start|>system\\nYou are a helpful assistant.<|im_end|>\\n<|im_start|>user\\n\\n你是一位企业分类的专家。请仔细阅读企业信息：武汉市青山区环宇日用品经营部:\\n经营范围:日用品销售及相关咨询推广服务;\\n大类名称: 批发和零售业;\\n中类名称: 零售业;\\n小类名称:纺织、服装及日用品专门零售;  \\n接下来，请从下述类别列表中选出最符合该企业信息的单一类别，并返回该类别名作为列表。  \\n若企业信息不属于下述任何类别，请返回['其他']。  \\n类别列表: ['光纤光缆、光器件/光模块、光系统设备等光通信', '芯片、半导体、存储器等集成电路', '显示器、液晶面板、触摸屏、虚拟现实、VR等显示', '手机、平板电脑、智能家居等智能终端', '5G网络', '互联网、云计算、大数据、物联网等', '传统汽车整车制造', '传统汽车关键核心零部件', '新能源整车制造', '新能源汽车关键核心零部件', '智能网联汽车配套产业', '汽车后市场服务', '医药流通', '生物制药、服务、制造等生物医药', '医疗器械', '生物农业', '健康服务', '3D打印与激光加工装备', '机器人', '高档数控机床', '绿色智能船舶、高技术船舶与海洋工程装备', '轨道交通装备', '工程机械装备', '高端能源装备', '金属新材料（含钢铁）', '石化化工新材料（含石化）', '无机非金属材料', '高性能纤维及制品和复合材料', '未来、前沿新材料', '电力、交通、建筑工程设计', '智能建造', '新零售、电子商务', '快递服务', '航空物流', '临港物流', '保税物流', '冷链物流', '货币金融服务', '证券服务', '投资服务', '保险业', '商务服务', '先进环保', '高效节能', '资源综合利用', '新型储能', '安全应急', '绿色消费品（含食品、纺织）', '滨江滨湖旅游', '夜色精品旅游', '生态休闲旅游', '红色文化旅游', '旅游演艺', '网络安全硬件', '网络安全软件（含元宇宙、区块链）', '网络安全服务', '航空维修及改装', '通航运营服务', '运载火箭及发射服务', '卫星制造及应用', '北斗基础构件', '北斗平台终端', '北斗应用服务', '人工智能芯片', '计算机视觉、自然语言处理、机器学习等AI技术', '无人机', '智能可穿戴设备', '智能机器人', '通用人工智能', '动漫设计', '游戏设计', '电竞直播', '设计服务', '数字传媒', '会议会展', '制氢、储氢及加氢等氢能设备', '电磁装备制造', '高速轨道交通', '量子通信', '量子计算', '量子测量', '超级计算', '类脑计算', '脑疾病诊疗', '脑机接口', '深地探测', '深海探测', '深空观测']  \\n返回格式：['类别名']  \\n<|im_end|>\\n<|im_start|>assistant\\n\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outputs[0].prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_data(pkl_item):\n",
    "    text = pkl_item.prompt\n",
    "    # 大类的企业:{industry_info}。\\n将它分类到如下类别:\n",
    "    item = re.search(\"请仔细阅读企业信息：(.*?)  \\n接下来\", text, re.DOTALL)\n",
    "    info = item.group(1)\n",
    "    label = pkl_item.outputs[0].text\n",
    "    return info, label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('武汉市青山区环宇日用品经营部:\\n经营范围:日用品销售及相关咨询推广服务;\\n大类名称: 批发和零售业;\\n中类名称: 零售业;\\n小类名称:纺织、服装及日用品专门零售;',\n",
       " \"['零售业']\")"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "extract_data(outputs[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in outputs:\n",
    "    print(extract_data(item)[0])\n",
    "    print(extract_data(item)[-1])\n",
    "    #\n",
    "    label = extract_data(item)[-1]\n",
    "    try:\n",
    "        label = eval(label)\n",
    "    except:\n",
    "        label = []\n",
    "    flag = []\n",
    "    for name in cls_name:\n",
    "        if name in label:\n",
    "            flag.append(name)\n",
    "    print(flag)\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {}\n",
    "for item in outputs:\n",
    "    label = extract_data(item)[-1]\n",
    "    try:\n",
    "        label = eval(label)\n",
    "    except:\n",
    "        continue\n",
    "    for name in cls_name:\n",
    "        if name in label:\n",
    "            d[name] = d.get(name, 0) + 1\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def see_domain(name):\n",
    "    for item in outputs:\n",
    "        info, label = extract_data(item)\n",
    "        # label = extract_data(item)[-1]\n",
    "        try:\n",
    "            label = eval(label)\n",
    "        except:\n",
    "            continue\n",
    "        if name in label:\n",
    "            print(info)\n",
    "            print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'新零售、电子商务': 14078,\n",
       " '商务服务': 12457,\n",
       " '轨道交通装备': 1245,\n",
       " '汽车后市场服务': 5985,\n",
       " '生态休闲旅游': 874,\n",
       " '互联网、云计算、大数据、物联网等': 2789,\n",
       " '工程机械装备': 2451,\n",
       " '健康服务': 4013,\n",
       " '设计服务': 2587,\n",
       " '快递服务': 661,\n",
       " '数字传媒': 1430,\n",
       " '智能建造': 6844,\n",
       " '传统汽车整车制造': 1409,\n",
       " '绿色消费品（含食品、纺织）': 4797,\n",
       " '高端能源装备': 276,\n",
       " '电力、交通、建筑工程设计': 1428,\n",
       " '金属新材料（含钢铁）': 4537,\n",
       " '动漫设计': 814,\n",
       " '会议会展': 1908,\n",
       " '绿色智能船舶、高技术船舶与海洋工程装备': 647,\n",
       " '货币金融服务': 576,\n",
       " '医疗器械': 1429,\n",
       " '传统汽车关键核心零部件': 983,\n",
       " '先进环保': 860,\n",
       " '无机非金属材料': 2197,\n",
       " '临港物流': 1004,\n",
       " '冷链物流': 1602,\n",
       " '医药流通': 2264,\n",
       " '手机、平板电脑、智能家居等智能终端': 166,\n",
       " '保险业': 230,\n",
       " '投资服务': 1170,\n",
       " '资源综合利用': 1526,\n",
       " '石化化工新材料（含石化）': 990,\n",
       " '高效节能': 379,\n",
       " '高档数控机床': 130,\n",
       " '智能网联汽车配套产业': 827,\n",
       " '机器人': 112,\n",
       " '生物农业': 2196,\n",
       " '航空物流': 272,\n",
       " '新能源整车制造': 695,\n",
       " '新能源汽车关键核心零部件': 970,\n",
       " '生物制药、服务、制造等生物医药': 212,\n",
       " '网络安全软件（含元宇宙、区块链）': 336,\n",
       " '网络安全服务': 353,\n",
       " '网络安全硬件': 130,\n",
       " '人工智能芯片': 111,\n",
       " '计算机视觉、自然语言处理、机器学习等AI技术': 105,\n",
       " '无人机': 92,\n",
       " '智能可穿戴设备': 107,\n",
       " '智能机器人': 296,\n",
       " '通用人工智能': 112,\n",
       " '安全应急': 214,\n",
       " '3D打印与激光加工装备': 111,\n",
       " '游戏设计': 629,\n",
       " '旅游演艺': 543,\n",
       " '电竞直播': 459,\n",
       " '新型储能': 125,\n",
       " '高性能纤维及制品和复合材料': 377,\n",
       " '证券服务': 185,\n",
       " '保税物流': 103,\n",
       " '光纤光缆、光器件/光模块、光系统设备等光通信': 26,\n",
       " '航空维修及改装': 26,\n",
       " '通航运营服务': 19,\n",
       " '运载火箭及发射服务': 3,\n",
       " '卫星制造及应用': 4,\n",
       " '北斗应用服务': 4,\n",
       " '5G网络': 46,\n",
       " '芯片、半导体、存储器等集成电路': 3,\n",
       " '滨江滨湖旅游': 4,\n",
       " '夜色精品旅游': 3,\n",
       " '红色文化旅游': 31,\n",
       " '制氢、储氢及加氢等氢能设备': 2,\n",
       " '显示器、液晶面板、触摸屏、虚拟现实、VR等显示': 8,\n",
       " '电磁装备制造': 10,\n",
       " '未来、前沿新材料': 6,\n",
       " '量子通信': 2,\n",
       " '北斗基础构件': 2,\n",
       " '北斗平台终端': 3,\n",
       " '量子计算': 2,\n",
       " '量子测量': 1,\n",
       " '脑疾病诊疗': 1,\n",
       " '脑机接口': 1,\n",
       " '高速轨道交通': 1}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "武汉观图地信测绘科技有限公司:\n",
      "经营范围:量子计算技术服务;计量技术服务;劳务服务;测绘服务;住宅室内装饰装修;\n",
      "大类名称: 科学研究和技术服务业;\n",
      "中类名称: 专业技术服务业;\n",
      "小类名称:测绘地理信息服务;\n",
      "\n",
      "武汉御歌者量子科技有限公司:\n",
      "经营范围:云计算装备技术服务;软件开发;信息技术咨询服务;量子计算技术服务;数据处理服务;基于云平台的业务外包服务;数字技术服务;动漫游戏开发;人工智能行业应用系统集成服务;信息系统集成服务;人工智能应用软件开发;人工智能双创服务平台;区块链技术相关软件和服务;物联网应用服务;3D打印基础材料销售;智能机器人销售;国内贸易代理;互联网销售;企业管理咨询;\n",
      "大类名称: 科学研究和技术服务业;\n",
      "中类名称: 专业技术服务业;\n",
      "小类名称:工业与专业设计及其他专业技术服务;\n",
      "\n"
     ]
    }
   ],
   "source": [
    "see_domain(\"量子计算\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
